//设置依赖的配置文件
require.config({
	paths : {
		"jquery" : "jquery",
		"cookie" : "cookie"
	}
})
//设置依赖
require(["jquery","cookie"],function($,cookiefn){
	$(function(){
		$(".top_cart .num span").html(cookiefn.getTotal());
		//动态获取页面元素
		$.getJSON('json/goods.json',function(response,status,xhr){
			//顶部活动区
			$(".active a").attr("href",response[0][0].links).children("img").attr("src",response[0][0].paths);
			//顶部特价
			$(".tejia a").attr("href",response[0][1].links).children("img").attr("src",response[0][1].paths);
			//banner广告
			$(".ad1 a").attr("href",response[0][2].links).children("img").attr("src",response[0][2].paths);		
			$(".ad2 a").attr("href",response[0][3].links).children("img").attr("src",response[0][3].paths);
			//右侧浮动栏广告
			$(".right_ad img").attr("src",response[0][4].paths);
			//banner+轮播
			$banner_date = response[1];
			$banner_index = $(".banner_index");
			$banner_con = $(".banner_list");
			//当前下标
			$current_index = 0;
			//添加banner及按钮
			for($i = 0;$i < $banner_date.length;$i ++){
				$banner_con.append("<li><a href='" + $banner_date[$i].links + "'><img src='" + $banner_date[$i].paths + "'/></a></li>");
				$banner_index.append("<span>" + ($i + 1) + "</span>");
			}
			//获取所有li-banner 及 下标按钮
			$banner_index_span = $(".banner_index span");
			$banner_con_li = $(".banner_list li");
			//开启轮播计时器
			$banner_timer = setInterval(banner_scroll,2500);
			function banner_scroll(){
				$banner_con_li.stop(true,true);
				$banner_index_span.css("background","#000");
				$banner_con_li.css({"opacity":"0","filter":"alpha(opacity=0)"});
				$banner_index_span.eq(++ $current_index % $banner_date.length).css("background","red");
				//$("#banner").css("background","#fff");
				$banner_con_li.eq($current_index % $banner_date.length).animate({opacity:1},500);
				$("#banner").css("background",$banner_date[($current_index % $banner_date.length)].bgcolor);
			}
			//鼠标移入banner时停止计时器
			$(".banner_con").mouseover(function(){
				clearInterval($banner_timer);
			}).mouseout(function(){
				$banner_timer = setInterval(banner_scroll,2500);
			})
			$banner_index_span.mouseover(function(){
				if($(this).index() != $current_index % $banner_date.length){
					$current_index = $(this).index() - 1;
					banner_scroll();
				}
			})
			$("#banner").hover(function(){
				$(".banner_btn").css("display","block");
			},function(){
				$(".banner_btn").css("display","none");
			});
			$(".banner_btn").find("i").hover(function(){
				$(this).index() == 0 ? $(this).css({"background-position":"0"}) : $(this).css({"background-position":"-42px"});
			},function(){
				$(this).index() == 0 ? $(this).css({"background-position":"-85px"}) : $(this).css({"background-position":"-126px"});
			})
			$(".banner_btn").find("i").click(function(){
				if($(this).index() == 0){
					$current_index -= 2;
					banner_scroll();
				}else{
					banner_scroll();
				}
			});
			//白酒优惠
			$bj_sale = $(".bj_sale li");
			$bj_sale_date = response[2];
			for($i = 0;$i < $bj_sale_date.length;$i ++){
				$bj_sale.eq($i).children("a").attr("href",$bj_sale_date[$i].links).children("img").attr("src",$bj_sale_date[$i].paths);
				$bj_sale.eq($i).find(".sale_fl").html($bj_sale_date[$i].terms + " " + $bj_sale_date[$i].message);
				$bj_sale.eq($i).find("i").html($bj_sale_date[$i].price);
			}
			//白酒折扣区-葡萄酒超值-洋酒热卖-右侧
			$wine_r = $(".wine_discount_r li");
			$wine_r_date = response[4];
			for($i = 0;$i < $wine_r_date.length;$i ++){
				$wine_r.eq($i).children("a").attr("href",$wine_r_date[$i].links).children("img").attr("src",$wine_r_date[$i].paths);
				$wine_r.eq($i).find(".wine_message").html($wine_r_date[$i].message);
				$wine_r.eq($i).find(".soft_terms").html($wine_r_date[$i].terms);
				$wine_r.eq($i).find(".price").html($wine_r_date[$i].price);
			}
			//白酒折扣区-葡萄酒超值-洋酒热卖-左侧
			$wine_l = $(".wine_discount_l li");
			$wine_l_date = response[3];
			for($i = 0;$i < $wine_l_date.length;$i ++){
				$wine_l.eq($i).children("a").attr("href",$wine_l_date[$i].links).children("img").attr("src",$wine_l_date[$i].paths);
			}
			//店长推荐
			$man_sug = $(".manager_recommend .sug_con li");
			$man_sug_date = response[5];
			for($i = 0;$i < $man_sug_date.length;$i ++){
				$man_sug.eq($i).children("a").attr("href",$man_sug_date[$i].links).children("img").attr("src",$man_sug_date[$i].paths);
				$man_sug.eq($i).find(".message").html($man_sug_date[$i].message);
				$man_sug.eq($i).find(".price").html($man_sug_date[$i].price);
				$man_sug.eq($i).find(".num").html($man_sug_date[$i].num);
			}
			//酒友热评
			$huar_evaluate = $(".huar_evaluate li");
			$huar_evaluate_date = response[6];
			for($i = 0;$i < $huar_evaluate_date.length;$i ++){
				$huar_evaluate.eq($i).children("a").attr("href",$huar_evaluate_date[$i].links).children("img").attr("src",$huar_evaluate_date[$i].paths);
				$huar_evaluate.eq($i).find(".message").html($huar_evaluate_date[$i].message);
				$huar_evaluate.eq($i).find(".price").html($huar_evaluate_date[$i].price);
				$huar_evaluate.eq($i).find(".details").html($huar_evaluate_date[$i].details);
				$huar_evaluate.eq($i).find(".fromwho").html($huar_evaluate_date[$i].fromwho);
			}
			//视频
			$vedios = $(".vedios li");
			$vedios_date = response[7];
			for($i = 0;$i < $vedios_date.length;$i ++){
				$vedios.eq($i).children("a").attr("href",$vedios_date[$i].links).children("img").attr("src",$vedios_date[$i].paths);
				$vedios.eq($i).find(".message").html($vedios_date[$i].message);
			}
		})
		//二级菜单
		
				
		//获取二级菜单的json
		$.getJSON('json/submenu.json',function(response,status,xhr){
			$(".submenu").mouseleave(function(){
				$(".submenu_con_list").empty();
				$(".submenu_con_img").empty();
				$(".submenu_con").css("display","none");
			});
			$(".submenu li").mouseenter(function(){
				//清空菜单面板
				$index = $(this).index();
				if($index <= 7){	
				
					$(".submenu_con_list").empty();
					$(".submenu_con_img").empty();
					$(".submenu_con").css("display","block");
					$littleImg = response[$index].littleImg;
					$bigImg = response[$index].bigImg;
					$first = response[$index].first;
					$second = response[$index].second;
					$third = response[$index].third;
					$fourth = response[$index].fourth;
					$fifth = response[$index].fifth;
					//alert($littleImg[1].path);
					if($littleImg.length > 0){
						$(".submenu_con_img").append("<div class='little_img clearfix'></div>");
						for($i = 0;$i < $littleImg.length;$i ++){
							$(".little_img").append("<a href='" + $littleImg[$i].link + "'><img src='" + $littleImg[$i].path + "'/></a>");
						}
					}
					if($bigImg.length > 0){
						$(".submenu_con_img").append("<div class='big_img clearfix'></div>")
						for($i = 0;$i < $bigImg.length;$i ++){
							$(".big_img").append("<a href='" + $bigImg[$i].link + "'><img src='" + $bigImg[$i].path + "'/></a>")
						}
					}
					$addEle($first,"first");
					$addEle($second,"second");
					$addEle($third,"third");
					$addEle($fourth,"fourth");
					$addEle($fifth,"fifth");
				}else{
					$(".submenu_con").css("display","none");
				}	
			})			
		})
		function $addEle($m,num){
			if($m){
				$(".submenu_con_list").append("<div class='" + num + "'></div>");
				$("." + num).append("<h2>" + $m[0] + "</h2><p></p>");
				for($i = 0;$i < $m[1].length;$i ++){
					$("." + num + " p").append("<a href='#'>" + $m[1][$i] + "</a>")
				}
			}
		}
		
		//右侧浮动工具栏
		$("#float_right").css("height",$(document).height());
		$("#float_right .right_con").css("top",($(window).height() - $(".right_con").height()) / 2);
		$(window).resize(function(){
			$("#float_right .right_con").css("top",($(window).height() - $(".right_con").height()) / 2);
		})
		$(".right_con li").hover(function(){
			//移入
			$(this).css("background","#c40000").children("div").css("display","block").animate({
				opacity:1,right:32
			},200);
			$(this).children("a").css("color","#fff");
		},function(){
			//移出
			$obj = $(this);
			$obj.css("background","#eaeaea").children("div").animate({
				opacity:0,right:80
			},200,function(){
				$obj.children("div").css("display","none");
			});
			$obj.children("a").css("color","#c40000");
		})
		//回到顶部
		$(".right_con li:last-child").click(function(){
			$('body,html').animate({scrollTop:0},500);
		})
		//顶部浮动栏
		$(window).scroll(float_top);
		$(document).ready(float_top);
		function float_top(){
			if($(window).scrollTop() >= 808){
				$("#float_top").css("display","block");
			}else{
				$("#float_top").css("display","none");
			}
		}
		//当鼠标移入商品时
		$gjw_goods = $(".goods li");
		$gjw_goods.hover(function(){
			//移入
			$(this).stop(true,true);
			$(this).animate({top:-5}).addClass("li_hover");
		},function(){
			//移出
			$(this).stop(true,true);
			$(this).animate({top:0}).removeClass("li_hover");			
		})
		//店长推荐轮播
		$manager_scroll = $(".sug_con ul");
		$manager_left = $(".scroll_btn span:first-child");
		$manager_right = $(".scroll_btn span:last-child");
		$manager_timer_left = null;
		$manager_timer_right = null;
		$manager_flag = false;
		fn_manager_scroll();
		function fn_manager_scroll(){
			$manager_timer_right = setTimeout(fn_manager_scroll_right,1500);
		};
		function fn_manager_scroll_left(){
			$(".scroll_btn .btn_click").removeClass("btn_click");
			$manager_left.addClass("btn_click");
			$manager_flag ? $manager_scroll.animate({"left":0},500) : $manager_scroll.animate({"left":0},500,fn_manager_scroll);
		}
		function fn_manager_scroll_right(){
			$(".scroll_btn .btn_click").removeClass("btn_click");
			$manager_right.addClass("btn_click");
			$manager_flag ? $manager_scroll.animate({"left":-1195},500) : $manager_scroll.animate({"left":-1195},500,function(){
				$manager_timer_left = setTimeout(fn_manager_scroll_left,1500)
			});
		}
		$(".scroll_btn span").click(function(){
			clearTimeout($manager_timer_right);
			clearTimeout($manager_timer_left);
			$manager_scroll.stop(true,true);
			if($(this).index() == 0){
				fn_manager_scroll_left();
			}else{
				fn_manager_scroll_right();
			}
		})
		$manager_scroll.hover(function(){
			$manager_flag = true;
			$manager_scroll.stop(true,true);
			clearTimeout($manager_timer_right);
			clearTimeout($manager_timer_left);
		})
		
		//底部滚动链接列表
		$yq_list_top = -34;
		(function yq_list_scroll(){
			$(".yq_list").animate({"top":$yq_list_top},2000,function(){
				if($yq_list_top < -67){
					$(".yq_list").css("top",0);
					setTimeout(function(){
						$yq_list_top = -34;
						yq_list_scroll();
					},1000)
				}else{
					setTimeout(function(){
						$yq_list_top += -34;
						yq_list_scroll();
					},1000)
				}
			});
		})();
	})
})
